Put the ipynb file and html file in the github branch you created in the last assignment and submit the link to the commit in brightspace
from plotly.offline import init_notebook_mode
import plotly.io as pio
import plotly.express as px
#Import pandas
import pandas as pd
init_notebook_mode(connected=True)
pio.renderers.default = "plotly_mimetype+notebook"
#load data
df = px.data.gapminder()
df.head()
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
#Copy code from the lab assignment 'Data Visualisation'
df2007 = df[df['year'] == 2007]
#Sum population per continent and make new dataframe
df2007continent = df2007.groupby('continent').sum()
df2007continent = pd.DataFrame(df2007continent)
df2007continent.head()
#Create figure and make different colors for each bar
#Order according to population
figurebar = px.bar(df2007continent, x='pop', orientation='h', color=df2007continent.index)
figurebar.update_layout(yaxis={'categoryorder':'total ascending'})
figurebar.show()
#Same as question 1
figurebar = px.bar(df2007continent, x='pop', orientation='h', color=df2007continent.index)
figurebar.update_layout(yaxis={'categoryorder':'total ascending'})
figurebar.show()
Add text to each bar that represents the population
#Same as question 1
figurebar = px.bar(df2007continent, x='pop', orientation='h', color=df2007continent.index)
figurebar.update_layout(yaxis={'categoryorder':'total ascending'})
figurebar.show()
Thus far we looked at data from one year (2007). Lets create an animation to see the population growth of the continents through the years
df = px.data.gapminder()
fig = px.histogram(df, x="pop", y="continent", animation_frame="year",
color="continent",
range_x = [0,4000000000] #Define range such that the graph evolves nicely over time
)
fig.update_yaxes(categoryorder = 'max ascending') #Bars in ascending order
fig.show()
Instead of the continents, lets look at individual countries. Create an animation that shows the population growth of the countries through the years
df = px.data.gapminder()
#Instead of y=continent we now use y=country
fig = px.histogram(df, x="pop", y="country", animation_frame="year",
color="country",
range_x = [0,4000000000] #Define range such that the graph evolves nicely over time
)
fig.update_yaxes(categoryorder = 'max ascending')
fig.show()
Clean up the country animation. Set the height size of the figure to 1000 to have a better view of the animation
df = px.data.gapminder()
#Instead of y=continent we now use y=country
fig = px.histogram(df, x="pop", y="country", animation_frame="year",
color="country",
range_x = [0,4000000000],
height = 1000
)
fig.update_yaxes(categoryorder = 'max ascending')
fig.show()
df = px.data.gapminder()
df['country'].nunique()
#There are 142 countries, so take 133 up and including 142
df = px.data.gapminder()
#Instead of y=continent we now use y=country
fig = px.histogram(df, x="pop", y="country", animation_frame="year",
color="country",
range_x = [0,4000000000],
range_y = [131.5,141.5], #Take top 10
height = 1000
)
fig.update_yaxes(categoryorder = 'max ascending')
fig.show()